package com.gopay.common.cps.dao.wallet.tyy.impl;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cps.dao.wallet.tyy.WaTyyBizOrderDtlDao;
import com.gopay.common.domain.dps.WaTyyBizOrderDtl;
import com.gopay.common.util.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.*;

/**
 * 天溢盈业务订单表
 * @author LEI XIAOBAO
 * @date 2016年9月22日
 */
@Repository(value="waTyyBizOrderDtlDao")
public class WaTyyBizOrderDtlDaoImpl extends BaseDAOImpl<WaTyyBizOrderDtl, Serializable> implements WaTyyBizOrderDtlDao {

	/**
	 * 获取Tyy Contract序列 add by byp 20160923
	 * @return
	 */
	@Override
	public String getWaTyyContractId() {
		return getNextId("WA_TYY_CONTRACT_ID_LAST_6_SEQ");

	}

	@Override
	public List<WaTyyBizOrderDtl> getOrderDtlByCondition(Map<String, Object> params) {
		String hql = "from WaTyyBizOrderDtl t where 1=1";
		for( Map.Entry<String, Object> entry : params.entrySet() ){
			hql += " AND "+entry.getKey()+"=:"+entry.getKey();
		}
		return this.findByHql(hql, params);
	}

	/**
     * 获取用户持有中的天溢盈业务订单信息
     *
     * @param custId
     * @return
     */
    @Override
    public List<WaTyyBizOrderDtl> getOrderDtlByCondition(String custId,int purchaseState,int pageIndex,int pageSize) {
//        long current=System.currentTimeMillis();//当前时间毫秒数
//        long zero=current/(1000*3600*24)*(1000*3600*24)- TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数              Date s=format.parse("12:00:00");
//        Date currtentDate=new Date(zero);
//        String date=DateUtils.format(currtentDate,"yyyy-MM-dd hh:mm:ss");
        Date currtDate=new Date();
        String date=DateUtils.format(DateUtils.add(currtDate, Calendar.DATE, 1),"yyyy-MM-dd");
        //到期时间大于次日零点显示在持有中产品列表中 modify by tangyang at 20170508
        String hql = "from WaTyyBizOrderDtl t where t.custId=:custId " +
                " and t.purchaseStatus=:purchaseStatus " +
                " and t.expirationDate > to_date(:currtentDate,'yyyy-MM-dd') "+
                " order by  t.purchaseTime desc ";
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("custId",custId);
        map.put("purchaseStatus",purchaseState);
        map.put("currtentDate",date);
        return this.findByHql(hql,map,pageIndex,pageSize);
    }

    /**
     * 获取用户持有中的天溢盈业务订单数量
     *
     * @param custId
     * @return
     */
    @Override
    public String getOrderDtlByConditionCount(String custId,int purchaseState) {
//        Date currtDate=new Date();
//        String date=DateUtils.format(DateUtils.add(currtDate, Calendar.DATE, 1),"yyyy-MM-dd");
        String hql = "from WaTyyBizOrderDtl t where t.custId=:custId " +
                " and t.purchaseStatus=:purchaseStatus " +
//                " and t.expirationDate > to_date(:currtentDate,'yyyy-MM-dd') "+
                " order by  t.purchaseTime desc ";
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("custId",custId);
        map.put("purchaseStatus",purchaseState);
//        map.put("currtentDate",date);
        List<WaTyyBizOrderDtl> list = findByHql(hql,map);
        return list!=null?(list.size()==0?"0":String.valueOf(list.size())):"0";
    }

    
	/**
	 * 根据订单号，查询天溢盈业务订单信息。
	 *
	 * @param orderId
	 * @return
	 */
	@Override
	public WaTyyBizOrderDtl getWaTyyBizOrderDtl(String orderId) {
		return this.find(WaTyyBizOrderDtl.class,orderId);
	}

	/**
	 * 根据国付宝理财支付订单号，查询天溢盈业务订单信息。
	 *
	 * @param gfborderId
	 * @return
	 */
	@Override
	public WaTyyBizOrderDtl getWaTyyBizOrderDtlByGFBPurchaseOrderId(String gfborderId) {
		String hql = "from WaTyyBizOrderDtl t where t.purchaseGopayOrderId=:purchaseGopayOrderId";
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("purchaseGopayOrderId",gfborderId);
		List<WaTyyBizOrderDtl> waTyyBizOrderDtlList = this.findByHql(hql,map);
		return waTyyBizOrderDtlList!=null?(waTyyBizOrderDtlList.size()>0?waTyyBizOrderDtlList.get(0):null):null;
	}

	/**
	 * 生成主键 byp 20161008
	 * @return
	 */
	@Override
	public String getOrderId() {
		return DateUtils.now2string("yyyyMMdd") + StringUtils.leftPad(getNextId("ORDER_ID_SEQ"),8, "0");
	}

    /**
     * 获取用户已完成的天溢盈业务订单信息
     *
     * @param custId
     * @return
     */
    @Override
    public List<WaTyyBizOrderDtl> getFinishOrderDtlByCondition(String custId,int purchaseState,int pageIndex,int pageSize) {
        Date currtDate=new Date();
        String date=DateUtils.format(DateUtils.add(currtDate, Calendar.DATE, 1),"yyyy-MM-dd");
        //到期时间小于等于次日零点显示在已完成产品列表中 modify by tangyang at 20170508
        String hql = "from WaTyyBizOrderDtl t where t.custId=:custId  " +
                " and t.purchaseStatus=:purchaseStatus " +
                " and t.expirationDate <= to_date(:currtentDate,'yyyy-MM-dd')  "+
                " order by  t.purchaseTime desc";
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("custId",custId);
        map.put("purchaseStatus",purchaseState);
        map.put("currtentDate",date);
        return this.findByHql(hql,map,pageIndex,pageSize);
    }
	/**
	 * 根据国付宝理财兑付本金订单号，查询天溢盈业务订单信息。
	 *
	 * @param gfborderId
	 * @return
	 */
	@Override
	public WaTyyBizOrderDtl getWaTyyBizOrderDtlByRepayGopayOrderId(String gfborderId) {
		String hql = "from WaTyyBizOrderDtl t where t.repayGopayOrderId=:repayGopayOrderId ";
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("repayGopayOrderId",gfborderId);
		List<WaTyyBizOrderDtl> waTyyBizOrderDtlList = this.findByHql(hql,map);
		return waTyyBizOrderDtlList!=null?(waTyyBizOrderDtlList.size()>0?waTyyBizOrderDtlList.get(0):null):null;
	}
	
	/**
	 * 根据国付宝理财兑付本金或利息订单号，查询天溢盈业务订单信息。
	 *
	 * @param gfborderId
	 * @return
	 * add by tanyang at 20170602
	 */
	@Override
	public WaTyyBizOrderDtl getWaTyyBizOrderDtlByOrderId(String gfborderId) {
		String hql = "from WaTyyBizOrderDtl t where t.repayGopayOrderId=:repayGopayOrderId or t.repayProfitGopayOrderId=:repayProfitGopayOrderId";
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("repayGopayOrderId",gfborderId);
		map.put("repayProfitGopayOrderId",gfborderId);
		List<WaTyyBizOrderDtl> waTyyBizOrderDtlList = this.findByHql(hql,map);
		return waTyyBizOrderDtlList!=null?(waTyyBizOrderDtlList.size()>0?waTyyBizOrderDtlList.get(0):null):null;
	}
	
	@Override
	public WaTyyBizOrderDtl getByContractId(String contractId) {
		String hql = "from WaTyyBizOrderDtl t where t.contractId=:contractId";
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("contractId",contractId);
		List<WaTyyBizOrderDtl> waTyyBizOrderDtlList = this.findByHql(hql,map);
		return waTyyBizOrderDtlList!=null?(waTyyBizOrderDtlList.size()>0?waTyyBizOrderDtlList.get(0):null):null;
	}
	
	
	/**
     * 根据custId查询是否成功购买过天溢盈
     * @param custId
     * @return
     */
    @Override
    public int getCountBycustId(String custId) {
        String hql = "from WaTyyBizOrderDtl t where t.custId=:custId " +
        		" and t.purchaseStatus =:purchaseStatus " ;
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("custId",custId);
        map.put("purchaseStatus",20000);
        List<WaTyyBizOrderDtl> list = this.findByHql(hql,map);
        if(list==null){
        	return 0;
        }
        return list.size();
    }
}
